/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.maze.behaviours;

import java.util.Random;
import org.maze.individual.Individual;

/**
 *
 * @author Thomas
 */
public abstract class AbstractImprovementBehaviour implements IImprovementBehaviour{
    protected static Random rand = new Random();

    protected byte[] replace(byte[] genome, int pos, int sizeReplaced, byte[] sequence) throws Exception{
        if(genome.length < sequence.length)
            throw new Exception("Wrong sequence size, > genome.length");
        if(pos<0)
            throw new Exception("Position is negative");
        if(sizeReplaced < sequence.length)
            throw new Exception("Wrong sequence size, > sizeReplaced");
        byte[] newGenome=genome.clone();
        System.arraycopy(sequence, 0, newGenome, pos, sequence.length);
        
        int copyPos = pos+sizeReplaced;
        int endOfNewSequence = pos+sequence.length;
        for(int i=0; i<newGenome.length-copyPos; i++){
            genome[i+endOfNewSequence]=genome[i+copyPos];
        }
        int endOfGenome = newGenome.length-copyPos;
        newGenome = Individual.fillBlank(newGenome, endOfGenome);
        return newGenome;
    }
}
